package com.dagger.weapon.service.impl;

import com.dagger.weapon.dao.UsersMapper;
import com.dagger.weapon.entity.Users;
import com.dagger.weapon.service.UserService;
import com.dagger.weapon.utils.MD5Utils;
import com.dagger.weapon.vo.ResStatus;
import com.dagger.weapon.vo.ResultVO;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;


    //这里没有调用工具类添加图片给了一个固定的地址
    private static final String DEFAULT_IMG ="http://localhost:8080/upload/";


    @Transactional
    public ResultVO userResgit(String name, String pwd) {

        synchronized (this) {
            //1.根据⽤户查询，这个⽤户是否已经被注册
            Example example = new Example(Users.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("username",name);
            List<Users> users = usersMapper.selectByExample(example);


            //2.如果没有被注册则进⾏保存操作
            if (users.size() == 0) {
                String md5Pwd = MD5Utils.md5(pwd);
                Users user = new Users();
                user.setUsername(name);
                user.setPassword(md5Pwd);
                user.setUserImg(DEFAULT_IMG+"default.png");
                user.setUserRegtime(new Date());
                user.setUserModtime(new Date());
                int i = usersMapper.insertUseGeneratedKeys(user);
                if (i > 0) {
                    return new ResultVO(ResStatus.OK, "注册成功！", user);
                } else {
                    return new ResultVO(ResStatus.NO, "注册失败！", null);
                }
            } else {
                return new ResultVO(ResStatus.NO, "⽤户名已经被注册！", null);
            }
        }
    }

    @Override
    public ResultVO checkLogin(String name, String pwd) {
        //1.根据账号查询用户信息(注意这里传的是对象)
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",name);
        List<Users> users = usersMapper.selectByExample(example);

        //2.判断
        if(users.size() == 0){
            //用户名不存在
            return new ResultVO(ResStatus.NO,"登录失败-用户名不存在",null);
        }else if(pwd==null){
            //没有输入密码-请输入
            return new ResultVO(ResStatus.NO,"没有输入密码-请输入",null);
        }else {
            //3.对输入的密码pwd进行加密
            String md5Pwd = MD5Utils.md5(pwd);
            //使用加密的密码 和 user中的密码进行匹配
            if(md5Pwd.equals(users.get(0).getPassword())){
                //jwt生成token字符串
                JwtBuilder builder = Jwts.builder();

                HashMap<String,Object> map = new HashMap<>();
                map.put("key1","value1");
                map.put("key2","value2");

                String token = builder.setSubject(name)
                        .setIssuedAt(new Date())
                        .setId(users.get(0).getUserId() + "")
                        .setClaims(map)
                        .setExpiration(new Date(System.currentTimeMillis() +24*60*60*1000))
                        .signWith(SignatureAlgorithm.HS256,"DAGGERweapon666")
                        .compact();

                //验证成功
                return new ResultVO(ResStatus.OK,token,users.get(0));
            }else {
                //密码错误
                return new ResultVO(ResStatus.NO,"登录失败，密码错误",null);
            }
        }
    }
}
